package com.jie.langlangmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.common.constant.MessageConstant;
import com.jie.langlangmall.common.exception.ApiException;
import com.jie.langlangmall.dao.PmsProductAttributeCategoryDao;
import com.jie.langlangmall.dto.PmsProductAttributeCategoryItem;
import com.jie.langlangmall.mapper.PmsProductAttributeCategoryMapper;
import com.jie.langlangmall.mapper.PmsProductAttributeMapper;
import com.jie.langlangmall.model.PmsProductAttributeCategory;
import com.jie.langlangmall.model.PmsProductAttributeCategoryExample;
import com.jie.langlangmall.model.PmsProductAttributeExample;
import com.jie.langlangmall.service.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/23
 * @Email 3217998214@qq.com
 * @Description 商品属性分类管理
 */
@Service
public class PmsProductAttributeCategoryServiceImpl implements PmsProductAttributeCategoryService {

    @Autowired
    private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;

    @Autowired
    private PmsProductAttributeCategoryDao productAttributeCategoryDao;

    @Autowired
    private PmsProductAttributeMapper productAttributeMapper;

    /**
     * 分页查询所有属性分组
     *
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<PmsProductAttributeCategory> list(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return productAttributeCategoryMapper.selectByExample(new PmsProductAttributeCategoryExample());
    }

    /**
     * 获取商品属性分组，包括组内的属性
     *
     * @return
     */
    @Override
    public List<PmsProductAttributeCategoryItem> listWithAttr() {
        return productAttributeCategoryDao.listWithAttr();
    }

    /**
     * 更细属性分组信息
     *
     * @param id
     * @param name
     */
    @Override
    public void update(Long id, String name) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setId(id);
        productAttributeCategory.setName(name);
        productAttributeCategoryMapper.updateByPrimaryKeySelective(productAttributeCategory);
    }

    /**
     * 删除单个商品属性分类
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        // 1. 如果该分类下关联了属性，不能删除
        PmsProductAttributeExample productAttributeExample = new PmsProductAttributeExample();
        productAttributeExample.createCriteria().andProductAttributeCategoryIdEqualTo(id);
        if (productAttributeMapper.countByExample(productAttributeExample) > 0) {
            throw new ApiException(MessageConstant.ATTRIBUTE_CATEGORY_EXIST_ATTRIBUTE);
        }
//        List<Long> attributeIds = productAttributeMapper.selectByExample(productAttributeExample)
//                .stream().map(PmsProductAttribute::getId).toList();
        // 2. 删除该分类下的所有属性
//        if (CollUtil.isNotEmpty(attributeIds)) {
//            productAttributeExample.clear();
//            productAttributeExample.createCriteria().andIdIn(attributeIds);
//            productAttributeMapper.deleteByExample(productAttributeExample);
//        }
        // 2. 删除该分类
        productAttributeCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 新增添商品属性分类
     *
     * @param name
     */
    @Override
    public void create(String name) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setName(name);
        if (productAttributeCategoryMapper.insertSelective(productAttributeCategory) <= 0) {
            throw new ApiException(MessageConstant.INSERT_FAILED);
        }
    }

    /**
     * 获取单个商品属性分类详情
     *
     * @param id
     * @return
     */
    @Override
    public PmsProductAttributeCategory getItem(Long id) {
        return productAttributeCategoryMapper.selectByPrimaryKey(id);
    }

}
